<?php

namespace App\Crontab;


use EasySwoole\Component\Di;
use EasySwoole\RedisPool\RedisPool;

class TaskExclusive
{

    public static function lock($key): bool
    {
        $key = str_replace("\\", ":", $key);
        $cache = RedisPool::defer();
        if(1 == $cache->setnx($key, 1)){
            $cache->expire($key, 59);
            return true;
        }else{
            \EasySwoole\EasySwoole\Logger::getInstance()->info('TaskExclusive: ' . $key . ' - ' . date('Y-m-d H:i:s'));
            return false;
        }
    }

}